﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace  RzCorportativo.Views.Cadastros
{
    public partial class Funcionarios : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Medico.SoAtivos = true;
            if (this.IsPostBack)
                return;
            if (this.Request.QueryString["funcionario"] != null)
            {
                this.Medico.LoadModel(new RzCorportativo.Lib.Medico() { MedicoId = EFramework.Web.TypeExtension.IntExtension.Parse(this.Request.QueryString["funcionario"], 0) });
            }
        }

        protected void salvar_Click(object sender, EventArgs e)
        {
            RzCorportativo.Lib.Medico medico = new RzCorportativo.Lib.Medico();
            medico = this.Medico.Model;
            Alert alert = new Alert("erroGeral", this);
            alert.AlertType = AlertType.Error;
            try
            {
                if (!this.Medico.IsValid)
                {
                    var msgs = this.Medico.GetErrors();
                    string msg = "Ocorreram os seguintes erros:";
                    int i = 0;
                    foreach (var item in msgs)
                    {
                        alert.Add(i.ToString(), item);
                        i += 1;
                    }
                    return;
                }
                bool result = medico.Salvar();
                if (result)
                {
                    Session["op"] = "s";
                    this.ClientScript.RegisterStartupScript(this.GetType(), "error", "window.location='" + EFramework.Web.UrlBase.UrlPadrao + "Views/Consulta/Funcionarios.aspx" + "'", true);
                }
                else
                {
                    alert.Add("falha", "Falha ao salvar o item");
                }
            }
            catch (Exception ex)
            {
                alert.Add("exception", ex.Message);
            }
            finally
            {
                if (alert.HasMessage)
                {
                    alert.Show();
                }
            }
        }
    }
}